package com.bifrost.util;

import org.apache.commons.lang.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lee
 * Create Class on 2018/5/28
 * @com.bifrost.util
 * @WMALL_DQ
 */
public class ServiceUtils {

    public static void findByExternalIds(List<Predicate> predicates, Root root, CriteriaBuilder cb, String externalIds , boolean isSkuCode) {
        if(StringUtils.isNotBlank(externalIds)){
            ArrayList<Predicate> externalIdOrs = new ArrayList<>();
            for (String externalId : externalIds.trim().split(",")) {
                if(isSkuCode){
                    externalIdOrs.add(cb.or(cb.like(root.get("externalId"),"%".concat(externalId).concat("%")),cb.like(root.get("skuCode"),"%".concat(externalId).concat("%"))));
                }else {
                    externalIdOrs.add(cb.like(root.get("externalId"),"%".concat(externalId).concat("%")));
                }
            }
            predicates.add(cb.or(externalIdOrs.toArray(new Predicate[externalIdOrs.size()])));
        }
    }
}
